package com.ihd.ihardware.view.tzc.health.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemRpBinding;
import com.ihd.ihardware.pojo.ReportRes;
import com.ihd.ihardware.pojo.Test_rp;
import com.ihd.ihardware.view.tzc.health.model.ReportRepository;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class ReportViewModel extends AndroidViewModel {

    private ReportRepository mReportRepository;

    public ReportViewModel(@NonNull Application application) {
        super(application);
    }


    private ObservableArrayList<Test_rp> mRP = new ObservableArrayList<>();
    public CommonAdapter mRPAdapter = new CommonAdapter<Test_rp, ItemRpBinding>(R.layout.item_rp, mRP, null) {
        @Override
        public void convert(ItemRpBinding mDataBinding, Test_rp rp, int position) {
            mDataBinding.icTV.setImageResource(rp.getIcon());

            if (position % 2 == 0)
                mDataBinding.concernItemLL.setBackgroundResource(R.color.C_FFFFFF);
            else
                mDataBinding.concernItemLL.setBackgroundResource(R.color.C_F7F7F7);

            if (TextUtils.isEmpty(rp.getDesc())) {
                mDataBinding.statusTV.setVisibility(View.INVISIBLE);
            } else {
                mDataBinding.statusTV.setVisibility(View.VISIBLE);
                mDataBinding.statusTV.setText(rp.getDesc());
                if ("瘦".equals(rp.getDesc()) || "消瘦".equals(rp.getDesc()) || "不足".equals(rp.getDesc()) || "偏低".equals(rp.getDesc()) || "未达标".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_0);
                } else if ("偏瘦".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_1);
                } else if ("标准".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_2);
                } else if ("偏胖".equals(rp.getDesc()) || "超重".equals(rp.getDesc()) || "超标".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_3);
                } else if ("肥胖".equals(rp.getDesc()) || "轻度".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_4);
                } else if ("中度".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_5);
                } else if ("极度肥胖".equals(rp.getDesc()) || "重度".equals(rp.getDesc()) || "过多".equals(rp.getDesc()) || "偏高".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_6);
                } else if ("优".equals(rp.getDesc()) || "达标".equals(rp.getDesc())) {
                    mDataBinding.statusTV.setBackgroundResource(R.drawable.corners_tag_7);
                } else {

                }
            }
        }
    };

    public void getRPList(ReportRes.DataBean rd) {
        if (mReportRepository == null)
            mReportRepository = new ReportRepository();
        mReportRepository.getRPList(mRP, rd);
    }

    public void getNorm(String id) {
        if (mReportRepository == null)
            mReportRepository = new ReportRepository();
        mReportRepository.norm(id);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mReportRepository) {
            mReportRepository.onDestroy();
            mReportRepository = null;
        }
    }
}
